package com.example.service;

import com.example.model.dto.MessageDTO;
import com.example.model.dto.PageDTO;
import com.example.model.entity.Message;
import com.example.repository.MessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MessageService {
    @Autowired
    private MessageRepository messageRepository;

    // 获取评论和@消息
    public PageDTO<MessageDTO> getCommentMessages(Long userId, Integer page, Integer pageSize) {
        Page<Message> messagePage = messageRepository.findByToUserIdAndTypeInOrderByCreatedAtDesc(
                userId,
                new String[]{"comment", "mention"},
                PageRequest.of(page - 1, pageSize)
        );
        
        return convertToPageDTO(messagePage, page, pageSize);
    }

    // 获取点赞和收藏消息
    public PageDTO<MessageDTO> getLikeAndCollectMessages(Long userId, Integer page, Integer pageSize) {
        Page<Message> messagePage = messageRepository.findByToUserIdAndTypeInOrderByCreatedAtDesc(
                userId,
                new String[]{"like", "collect"},
                PageRequest.of(page - 1, pageSize)
        );
        
        return convertToPageDTO(messagePage, page, pageSize);
    }

    // 获取关注消息
    public PageDTO<MessageDTO> getFollowMessages(Long userId, Integer page, Integer pageSize) {
        Page<Message> messagePage = messageRepository.findByToUserIdAndTypeInOrderByCreatedAtDesc(
                userId,
                new String[]{"follow"},
                PageRequest.of(page - 1, pageSize)
        );
        
        return convertToPageDTO(messagePage, page, pageSize);
    }

    private PageDTO<MessageDTO> convertToPageDTO(Page<Message> messagePage, Integer page, Integer pageSize) {
        List<MessageDTO> messageDTOs = messagePage.getContent().stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
        
        PageDTO<MessageDTO> pageDTO = new PageDTO<>();
        pageDTO.setRecords(messageDTOs);
        pageDTO.setTotal(messagePage.getTotalElements());
        pageDTO.setSize(pageSize);
        pageDTO.setCurrent(page);
        
        return pageDTO;
    }

    private MessageDTO convertToDTO(Message message) {
        MessageDTO dto = new MessageDTO();
        dto.setId(message.getId());
        dto.setAvatar(message.getFromUser().getAvatar());
        dto.setUsername(message.getFromUser().getUsername());
        dto.setTime(message.getCreatedAt());
        dto.setType(getTypeDescription(message.getType()));
        dto.setContent(message.getContent());
        
        if (message.getRelatedPost() != null) {
            dto.setRelatedImage(message.getRelatedPost().getImageUrls());
        }
        
        return dto;
    }

    private String getTypeDescription(String type) {
        switch (type) {
            case "comment":
                return "评论了你的笔记";
            case "mention":
                return "@了你";
            case "like":
                return "赞了你的笔记";
            case "collect":
                return "收藏了你的笔记";
            case "follow":
                return "关注了你";
            default:
                return type;
        }
    }
} 